package name.bingo.bing.pay.web.acid.front;

import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import name.bingo.bing.pay.web.acid.model.AcAccount;
import name.bingo.bing.pay.web.acid.service.AcAccountReadService;
import name.bingo.bing.pay.web.acid.service.AcAccountWriteService;
import name.bingo.common.model.Paging;
import name.bingo.common.utils.RespHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author [ your email ]
 * @date 2019-03-19 14:11:53
 * Created by CodeGen .
 */
@Api(tags = {""})
@Slf4j
@RestController
@RequestMapping("acid")
public class AcAccounts {

    @Autowired
    private AcAccountWriteService acAccountWriteService;

    @Autowired
    private AcAccountReadService acAccountReadService;

    @ApiOperation("查询")
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AcAccount findAcAccount(@PathVariable Long id) {
        return RespHelper.or500(acAccountReadService.findById(id));
    }

    @ApiOperation("分页〉")
    @GetMapping(value = "/paging", produces = MediaType.APPLICATION_JSON_VALUE)
    public Paging<AcAccount> pagingAcAccount(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                                             @RequestParam(value = "pageSize", required = false) Integer pageSize) {

        Map<String, Object> criteria = Maps.newHashMap();
        return RespHelper.or500(acAccountReadService.paging(pageNo, pageSize, criteria));
    }

    @ApiOperation("新建")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public Long createAcAccount(@RequestBody AcAccount acAccount) {
        return RespHelper.or500(acAccountWriteService.create(acAccount));
    }

    @ApiOperation("新建")
    @GetMapping("init")
    public Long initAcAccount() {
        long millis=System.currentTimeMillis();
        long random=(long)(1+Math.random()*(100-1+1));

        AcAccount acAccount=new AcAccount();
        acAccount.setCustomerId(millis%10000+random);
        acAccount.setCustomerCode(""+acAccount.getCustomerId());
        acAccount.setCustomerName("正泰网络"+acAccount.getCustomerId());
        acAccount.setAccountType("bank");
        acAccount.setBusinessType("o2o");
        acAccount.setCompanyId((millis%10000+random)%2 ==1 ? 101L :100L);
        acAccount.setCompanyName((millis%10000+random)%2 ==1 ? "网络技术" :"民用电器");
        acAccount.setCreditLoanAmount(new BigDecimal(10000));
        acAccount.setNegativeAmount(new BigDecimal(10000));
        acAccount.setBalance(new BigDecimal(10000));
        acAccount.setFreezingAmount(new BigDecimal(0));
        acAccount.setCreateUserId(1L);
        acAccount.setModifyUserId(1L);
        acAccount.setCreateUserName("网络管理员");
        acAccount.setModifyUserName("网络管理员");
        acAccount.setIsActive(1);
        acAccount.setIsDeleted(1);
        acAccount.setVersionNo(0);

        return RespHelper.or500(acAccountWriteService.create(acAccount));
    }


    @ApiOperation("更新")
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public Boolean updateAcAccount(@RequestBody AcAccount acAccount) {
        return RespHelper.or500(acAccountWriteService.update(acAccount));
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Boolean deleteAcAccount(@PathVariable Long id) {
        return false;
    }

    @ApiOperation("列表〃")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<AcAccount> listAcAccount() {
        Map<String, Object> criteria = Maps.newHashMap();
        return RespHelper.or500(acAccountReadService.list(criteria));
    }
}
